package com.kingdee.openimport.patch_service.service.impl;

import com.kingdee.openimport.patch_service.dao.PatchVersionRepository;
import com.kingdee.openimport.patch_service.model.PatchVersion;
import com.kingdee.openimport.patch_service.service.IPatchVersionService;
import com.kingdee.openimport.patch_service.service.IRedisCacheService;
import com.kingdee.openimport.patch_service.util.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Niki on 2018/4/21 10:49
 */
@Service
public class PatchVersionServiceImpl implements IPatchVersionService {
    @Resource
    private PatchVersionRepository patchVersionRepository;
    @Resource
    private IRedisCacheService redisCacheService;

    @Override
    public void createPatchVersion(PatchVersion patchVersion) {
        patchVersionRepository.save(patchVersion);
        //修改最新版本的
        redisCacheService.setLatesVersion(patchVersion.getVersion());
    }

    @Override
    public List<PatchVersion> findAllPatchVersion() {
        return patchVersionRepository.findAll();
    }

    @Override
    public List<String> findLatestVersion(int size) {
        Pageable pageable = new PageRequest(0, size);

        Page<String> result = patchVersionRepository.getVersions(pageable);
        if (result.getTotalElements() <= 0) {
            return null;
        }
        return result.getContent();
    }

    @Override
    public boolean exist(String version) {
        if (StringUtils.isBlank(version)) {
            return false;
        }
        PatchVersion patchVersion = patchVersionRepository.findByVersion(version);
        return patchVersion != null && version.equals(patchVersion.getVersion());
    }
}
